﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Obout.ComboBox;

namespace VirtualOrg.Web
{
    public partial class FormListProject : OboutInc.oboutAJAXPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetCategories();
            }
        }

        private void GetCategories()
        {
            using (DB.DbStoreDataContext dataContext = new DB.DbStoreDataContext())
            {
                ddlCategory.DataSource = dataContext.GetCategories("ORG_TYPE");
                ddlCategory.DataTextField = "Title";
                ddlCategory.DataValueField = "CategoryID";
                ddlCategory.DataBind();
                ddlCategory.Items.Add("Other");
                ddlCategory.Items.Insert(0, "");
            }
        }

        public string CreateOrg(string title, string tagline, string description, int categoryId, int subcategoryId, string otherCategory)
        {
            MessageBox msgbox = null;
            string resultmessage;
            try
            {
                if (CheckOrgName(title))
                {
                    using (DB.DbStoreDataContext dataContext = new DB.DbStoreDataContext())
                    {
                        if (!dataContext.CheckEmployeeValidityToCreateOrg(1).Value)
                        {
                            dataContext.CreateOrganization(title, tagline, description, "", "", 1, categoryId, subcategoryId, otherCategory);
                            resultmessage = "true";
                        }
                        else
                        {
                            msgbox = new MessageBox("Your organization limit has reached!");
                            resultmessage = msgbox.ReturnObject();
                        }
                    }
                }
                else
                {
                    msgbox = new MessageBox("Organization name already exists!");
                    resultmessage = msgbox.ReturnObject();
                }
            }
            catch
            {
                msgbox = new MessageBox("Error while creating organization. Please try again later!");
                resultmessage = msgbox.ReturnObject();
            }

            return resultmessage;
        }

        public bool CheckOrgName(string searchText)
        {
            bool result = true;
            using (DB.DbStoreDataContext dataContext = new DB.DbStoreDataContext())
            {
                int? orgcount = dataContext.CheckOrgnameExist(searchText);
                if (orgcount.Value > 0)
                {
                    result = false;
                }
            }

            return result;
        }

        protected void ddlCategory_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtOtherCategory.Text = "";
            ddlSubCategory.Items.Clear();

            if (ddlCategory.SelectedItem.Text != "")
            {
                if (ddlCategory.SelectedItem.Text != "Other")
                {
                    using (DB.DbStoreDataContext dataContext = new DB.DbStoreDataContext())
                    {
                        ddlSubCategory.DataSource = dataContext.GetSubCategories(Convert.ToInt64(ddlCategory.SelectedValue));
                        ddlSubCategory.DataTextField = "Title";
                        ddlSubCategory.DataValueField = "SubCategoryID";
                        ddlSubCategory.DataBind();
                        ddlSubCategory.Items.Insert(0, "");
                    }

                    txtOtherCategory.Visible = false;
                    ddlSubCategory.Visible = true;
                }
                else
                {
                    txtOtherCategory.Visible = true;
                    ddlSubCategory.Visible = false;
                }
            }
            else
            {
                txtOtherCategory.Visible = false;
                ddlSubCategory.Visible = true;
            }
        }
    }
}